package com.emm.yixun.website.controller.estate;
//框架包

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
//common
import com.emm.yixun.common.page.PageUtil;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.model.User;
//website
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;
import com.emm.yixun.common.model.Project;
//变化的引用
import com.emm.yixun.common.model.ProjectParameter;
import com.emm.yixun.common.model.dto.ProjectDto;
import com.emm.yixun.common.model.dto.ProjectParameterDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.estate.service.ProjectParameterServiceFacade;
import com.emm.yixun.estate.service.ProjectServiceFacade;

@Controller
@RequestMapping("/estate/projectParameter/")
public class ProjectParameterController extends BaseController<ProjectParameter> {

    @Autowired
    private ProjectParameterServiceFacade projectParameterServiceFacade;

    @Autowired
    private ProjectServiceFacade projectServiceFacade;

    @RequestMapping(value = "index")
    public String index(ModelMap map) {
        if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
            //查询菜单按钮权限
            map.put("permitBtn", getPermitBtn(Const.RESOURCES_TYPE_FUNCTION));

            //查询项目
            User user = ShiroUtil.getCurrentUser();
            ProjectDto entity = new ProjectDto();
            entity.setMerchantId(user.getMerchantId());
            entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
            entity.setResourcesCode("menu_0003_0001");
            entity.setPermissionUserId(user.getId());
            ResponseList<Project> res = projectServiceFacade.findByDto(entity);
            List<Project> projectList = new ArrayList<Project>();
            if (res.isSuccess() && null != res.getEntity() && res.getEntity().size() > 0) {
                projectList = res.getEntity();
            }
            map.addAttribute("projectList", projectList);

            return "/estate/projectParameter/index";
        }
        return Const.NO_AUTHORIZED_URL;
    }

    //新增返回实体对象
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRes add(ProjectParameterDto dto, HttpServletRequest request) {
        AjaxRes ar = getAjaxRes();

        /*
         *1.判断权限
         *2.调用新增接口
         *3.返回信息
         */
        //if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:projectParameter:add"))){
        try {
            String json = request.getParameter("json");
            User user = ShiroUtil.getCurrentUser();
            dto.setMerchantId(user.getMerchantId());
            dto.setCreateUserId(user.getId());
            dto.setDelFlag("0");
            Response<ProjectParameterDto> response = projectParameterServiceFacade.save(dto, json);
            if (response.isSuccess()) {
                ar.setSucceedMsg(Const.SAVE_SUCCEED);
            } else {
                ar.setFailMsg(Const.SAVE_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            ar.setFailMsg(Const.SAVE_FAIL);
        }
        //}
        return ar;
    }

    //新增返回实体对象
    @RequestMapping(value = "addSingleParam", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRes addSingleParam(ProjectParameterDto dto, HttpServletRequest request) {
        AjaxRes ar = getAjaxRes();

        /*
         *1.判断权限
         *2.调用新增接口
         *3.返回信息
         */
        //if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:projectParameter:add"))){
        try {
            String json = request.getParameter("json");
            User user = ShiroUtil.getCurrentUser();
            dto.setMerchantId(user.getMerchantId());
            dto.setCreateUserId(user.getId());
            dto.setDelFlag("0");
            Response<ProjectParameterDto> response = projectParameterServiceFacade.saveSingleParam(dto, json);
            if (response.isSuccess()) {
                ar.setSucceedMsg(Const.SAVE_SUCCEED);
            } else {
                ar.setFailMsg(Const.SAVE_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            ar.setFailMsg(Const.SAVE_FAIL);
        }
        //}
        return ar;
    }

    //查看
    @RequestMapping(value = "view", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRes view(Long id) {
        AjaxRes ar = getAjaxRes();
        /*
         *1.判断权限
         *2.调用查看接口
         *3.返回信息
         */
        if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "estate:projectParameter:view"))) {
            try {
                Response<ProjectParameter> response = projectParameterServiceFacade.findById(id);
                if (response.isSuccess()) {
                    ar.setSucceed(response.getEntity());
                } else {
                    ar.setFailMsg(Const.DATA_FAIL);
                }
            } catch (Exception e) {
                logger.error(e.toString(), e);
                ar.setFailMsg(Const.DATA_FAIL);
            }
        }
        return ar;
    }

    //修改
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRes edit(@RequestBody ProjectParameterDto dto) {
        AjaxRes ar = getAjaxRes();
        /*
         *1.判断权限
         *2.调用修改接口
         *3.返回信息
         */
//        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:projectParameter:edit"))){
        try {
            if(dto.getCycleValue() == null || Integer.valueOf(dto.getCycleValue())<0){
                ar.setFailMsg("cycleValue error");
            }else{
                Response<Integer> response = projectParameterServiceFacade.updateByPrimaryKey(dto, true);
                if (response.isSuccess()) {
                    ar.setSucceedMsg(Const.UPDATE_SUCCEED);
                } else {
                    ar.setFailMsg(Const.UPDATE_FAIL);
                }
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            ar.setFailMsg(Const.UPDATE_FAIL);
        }
        return ar;
    }

    //删除
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRes delete(Long id) {
        AjaxRes ar = getAjaxRes();
        /*
         *1.判断权限
         *2.调用查看接口
         *3.返回信息
         */
        if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "estate:projectParameter:delete"))) {
            try {
                Response<Integer> response = projectParameterServiceFacade.deleteByPrimaryKey(id);
                if (response.isSuccess()) {
                    ar.setSucceedMsg(Const.DEL_SUCCEED);
                } else {
                    ar.setFailMsg(Const.DEL_FAIL);
                }
            } catch (Exception e) {
                logger.error(e.toString(), e);
                ar.setFailMsg(Const.DEL_FAIL);
            }
        }
        return ar;
    }

    //分页查询
    @RequestMapping(value = "getPage", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRes getPage(ProjectParameterDto dto, Integer pageSize, Integer pageNum) {
        AjaxRes ar = getAjaxRes();
        /*
         *1.判断权限
         *2.调用分页查询接口
         *3.返回信息
         */
        //if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:projectParameter:list"))){
        try {
            Page<ProjectParameterDto> response = projectParameterServiceFacade.findPageByDto(dto, PageUtil.begin(pageNum, pageSize), pageSize);
            if (response.getSuccess()) {
                if (response.getRows() == null) {
                    User user = ShiroUtil.getCurrentUser();
                    dto.setMerchantId(user.getMerchantId());
                    dto.setCreateUserId(user.getId());
                    projectParameterServiceFacade.init(dto);
                    response = projectParameterServiceFacade.findPageByDto(dto, PageUtil.begin(pageNum, pageSize), pageSize);
                }
                response.setPageNumber(pageNum);
                ar.setSucceed(response);
            } else {
                ar.setFailMsg(Const.DATA_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            ar.setFailMsg(Const.DATA_FAIL);
        }
        //}
        return ar;
    }

    /**
     * 查询项目下的业态
     *
     * @param projectId
     * @return
     */
    @RequestMapping(value = "getIndustMarkByProjectId", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRes getIndustMarkByProjectId(Long projectId) {
        AjaxRes ar = getAjaxRes();
        /*
         *1.判断权限
         *2.调用查询接口
         *3.返回信息
         */
        //if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:projectParameter:list"))){
        try {
            ResponseList<ProjectParameterDto> response = projectParameterServiceFacade.getIndustMarkByProjectId(projectId);
            if (!response.isSuccess()) {
                ar.setFailMsg(Const.DATA_FAIL);
                return ar;
            }
            ar.setSucceed(response);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            ar.setFailMsg(Const.DATA_FAIL);
        }
        //}
        return ar;
    }

    /**
     * 查询所选项目的设置参数
     *
     * @param projectId
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRes list(Long projectId) {
        AjaxRes ar = getAjaxRes();
        /*
         *1.判断权限
         *2.调用列表查询接口
         *3.返回信息
         */
        //if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:projectParameter:list"))){
        try {
            ProjectParameterDto dto = new ProjectParameterDto();
            dto.setProjectId(projectId);
            dto.setDelFlag(DelFlagEnum.UNDEL.getNo());
            ResponseList<ProjectParameter> response = projectParameterServiceFacade.findListByDto(dto);
            if (!response.isSuccess()) {
                ar.setFailMsg(Const.DATA_FAIL);
                return ar;
            }
            if (null == response.getEntity()) {
                response.setEntity(new ArrayList<ProjectParameter>());
            }
            ar.setSucceed(response);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            ar.setFailMsg(Const.DATA_FAIL);
        }
        //}
        return ar;
    }

    /**
     * 根据业态和项目ID，查询项目参数设置
     *
     * @param typeId
     * @param projectId
     * @param cycleType
     * @return
     */
    @RequestMapping(value = "getProjectParameter", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRes getProjectParameter(String typeId, Long projectId, String cycleType) {
        AjaxRes ar = getAjaxRes();
        //现在修改为分三类客户自动放弃周期、认购转签约周期、签约收款日期分类设置，不再使用业态
        typeId = "0";
        /*
         *1.判断权限
         *2.调用查看接口
         *3.返回信息
         */
        //if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:projectParameter:view"))){
        try {
            ProjectParameterDto entity = new ProjectParameterDto();
            entity.setType(typeId);
            entity.setProjectId(projectId);
            entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
            entity.setCycleType(cycleType);
            Response<ProjectParameter> response = projectParameterServiceFacade.findByDto(entity);
            if (response.isSuccess()) {
                ar.setSucceed(response.getEntity());
            } else {
                ar.setFailMsg(Const.DATA_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            ar.setFailMsg(Const.DATA_FAIL);
        }
        //}
        return ar;
    }
}
